﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Reflection;
using System.Linq;
using System.Text;
using Lswweb.Domain;
using Lswweb.IDAL;
using Lswweb.Model;

namespace Lswweb.Domain
{
    public class FlashBanner
    {
        public FlashBanner(string title, string imageUrl, string linkUrl)
        {
            this.Title = title;
            this.ImageUrl = imageUrl;
            this.LinkUrl = linkUrl;
        }

        public string ImageUrl { get; private set; }

        public string LinkUrl { get; private set; }

        public string Title { get; private set; }
    }

    public abstract class FlashBannerCollection
    {
        private IList<FlashBanner> _banners;

        public FlashBanner this[int index]
        {
            get { return _banners[index]; }
        }

        public string ImageUrls
        {
            get { return BuilderString("ImageUrl"); }
        }

        public string LinkUrls
        {
            get { return BuilderString("LinkUrl"); }
        }

        public string Titles
        {
            get { return BuilderString("Title"); }
        }

        protected abstract IList<FlashBanner> BuilderBanners();

        private string BuilderString(string propertyName)
        {
            if(this._banners==null)
                this._banners = BuilderBanners();

            StringBuilder strBuilder=new StringBuilder();
            foreach (FlashBanner banner in _banners)
            {
                PropertyInfo property=  banner.GetType().GetProperty(propertyName);
                strBuilder.Append((string)property.GetValue(banner, null));
                strBuilder.Append("|");
            }

            return strBuilder.ToString().TrimEnd('|');
        }
    }

    public class UpFileFlashBannerCollection : FlashBannerCollection
    {
        private IUpFiles _upFileDAL;
        private int _itemType;

        public UpFileFlashBannerCollection(int categoryId)
            : this(DALFactory.CreateUpFilesDAL(), categoryId) { }

        public UpFileFlashBannerCollection(IUpFiles upFileDAL, int categoryId)
        {
            this._upFileDAL = upFileDAL;
            this._itemType = categoryId;
        }

        protected override IList<FlashBanner> BuilderBanners()
        {
            IList<FlashBanner> banners = new List<FlashBanner>();
            int total;
            IList<UpFileInfo> files = this._upFileDAL.GetFiles(string.Empty, (int)this._itemType, SiteContext.Current.Language,StatusType.Enabled, 1, 999, out total);
            foreach (UpFileInfo file in files)
                banners.Add(new FlashBanner(file.FileName, file.FileUrl, file.LinkUrl));

            return banners;
        }
    }

    public class NewsFlashBannerCollection : FlashBannerCollection
    {
        private INews _dal;
        private int _categoryId=0;
        private BooleanType _isHot = BooleanType.None;
        private BooleanType _isTop = BooleanType.None;
        public event Func<NewsInfo,string> BuilderLinkUrl;

        public NewsFlashBannerCollection(int categoryId, BooleanType isHot, BooleanType isTop)
            : this(DALFactory.CreateNewsDAL(), categoryId, isHot,isTop) { }

        public NewsFlashBannerCollection(INews newsDAL, int categoryId, BooleanType isHot, BooleanType isTop)
        {
            this._dal = newsDAL;
            this._categoryId = categoryId;
            this._isHot = isHot;
            this._isTop = isTop;
        }

        protected override IList<FlashBanner> BuilderBanners()
        {
            IList<FlashBanner> banners = new List<FlashBanner>();
            int total;
            IList<NewsInfo> files = this._dal.GetNewsList(string.Empty, SearchKeyType.Title,_categoryId, StatusType.Enabled, AuditingStatusType.Approved,_isHot,_isTop,DateTime.MaxValue,DateTime.MinValue, SiteContext.Current.Language, 1, 999, out total);
            foreach (NewsInfo file in files)
                banners.Add(new FlashBanner(file.Title, file.ImageUrl, BuilderLinkUrl(file)));

            return banners;
        }
    }
}
